<?php

/*
*跟进记录接口类
*/

class followAppClass extends appBase
{
	/*查询条件*/
	public function search()
	{
		//权限
		if ($this->power("follow", "seeAll")) {
			$stid = "是";
		} else {
			$stid = "否";
		}
		//查询条件
		$json['search'] = array(
			"stid"   => $stid,
			"target" => array("事务", "客户", "订单"),//对象
		);
		//返回
		$json['warn'] = "success";
		return $json;
	}
	
	/*知识管理-跟进记录-列表页*/
	public function home()
	{
		//like查询数组
		$like = array(
			"text" => $this->post['text'],//跟进内容
		);
		//全等查询数组
		$equal = array(
			"target" => $this->post['target'],//对象
			"stid"   => $this->post['stid'],//跟进员工
		);
		//如果没有查看所有员工跟进记录的权限
		if (!$this->power("follow", "seeAll")) {
			$and = " and stid = '" . $this->control['stid'] . "' ";
		}
		//获取查询结果
		$where = search(array("like" => $like, "=" => $equal)) . $and;
		$page  = $this->page(array("table" => "follow", "where" => $where, "order" => "time desc"));
		//数据准备
		$json['follow'] = array();
		foreach ($page['array'] as $array) {
			$staff = query("staff", "stid = '$array[stid]'");
			if ($array['target'] == "客户") {
				$kehu       = query("kehu", "khid = '$array[targetId]'");
				$targetName = $kehu['companyName'];
			} elseif ($array['target'] == "订单") {
				$buyCar     = query("buyCar", "id = '$array[targetId]'");
				$targetName = $buyCar['name'];
			}
			$json['follow'][] = array(
				"staffName"  => $staff['name'],//跟进人
				"target"     => $array['target'],//跟进对象
				"targetId"   => $array['targetId'],//对象ID
				"targetName" => $targetName,//对象名称
				"text"       => $array['text'],//跟进内容
				"time"       => $array['time'],//跟进时间
			);
		}
		//返回
		$json['page'] = $page['para'];
		$json['warn'] = "success";
		return $json;
	}
	
	/*跟进记录预想输入*/
	public function followQuick()
	{
		$json['para'] = explode("，", para("followQuick"));
		$json['warn'] = "success";
		return $json;
	}
	
	/*新增跟进记录*/
	public function add()
	{
		//赋值
		$backlogType = $this->post['target'] . "跟进";                            //待办事项类型
		$followText  = $this->post['text'] . "【" . $this->control['name'] . "】";//添加后缀，防止管理员被删除后无法通过管理员id查询到管理员姓名
		$date        = date("Y-m-d");
		$field       = array(
			"id"       => $this->post['id'],//跟进ID
			"stid"     => $this->control['stid'],//跟进人
			"target"   => $this->post['target'],//跟进对象
			"targetId" => $this->post['targetId'],//跟进对象ID号
			"text"     => $followText,
			"time"     => date("Y-m-d H:i:s"),//创建时间
		);
		//是否存在相同的下次跟进日期提醒计划
		$repeatNum = sqlNum("backlog", " stid = '" . $this->control['stid'] . "' and type = '$backlogType' and typeId = '$field[targetId]' and startDay = '" . $this->post['startDay'] . "' ");
		//当天是否提交相同内容的跟进信息
		$repeatTxt = sqlNum("follow", " stid = '" . $this->control['stid'] . "' and target = '$field[target]' and targetId = '$field[targetId]' and text = '$followText' and time > '$date' ");
		//判断
		if (empty($field['target'])) {
			$json['warn'] = "跟进对象为空";
		} elseif (empty($field['targetId'])) {
			$json['warn'] = "跟进对象ID号为空";
		} elseif (empty($this->post['text'])) {
			$json['warn'] = "请填写跟进信息";
		} elseif (!empty($this->post['startDay']) and $this->post['startDay'] < $date) {
			$json['warn'] = "下次跟进日期不得小于今天";
		} elseif (!empty($this->post['startDay']) and $repeatNum > 0) {
			$json['warn'] = "存在相同的下次跟进日期提醒计划";
		} elseif ($repeatTxt > 0) {
			$json['warn'] = "您当天不能提交相同内容的跟进信息";
//		}elseif($field['target'] == "事务"){
//			if(!$this->power("affair","followAdd")){
//				$json['warn'] = "你没有新增工作事务跟进的权限";
//			}else{
//				$affair = query("affair"," id = '$field[targetId]' ");
//				if(empty($affair['id'])){
//					$json['warn'] = "未找到此工作任务";
//				}else{
//					$finger = 2;
//					//更新任务更新时间
//					update(array(
//						"table" => "affair",
//						"field" => array("updateTime" => date("Y-m-d H:i:s"),),
//						"where" => " id = '$field[targetId]' ",
//					));
//				}
//			}
//			$name = $affair['name'];
		} elseif ($field['target'] == "客户") {
			if (!$this->power("client", "newFollow")) {
				$json['warn'] = "你没有新增客户跟进的权限";
			} else {
				$kehu = query("kehu", " khid = '$field[targetId]' ");
				if (empty($kehu['khid'])) {
					$json['warn'] = "未找到此客户";
				} else {
					$finger = 2;
					//更新客户更新时间
					update(array(
						"table" => "kehu",
						"field" => array("updateTime" => date("Y-m-d H:i:s"),),
						"where" => " khid = '$field[targetId]' ",
					));
				}
			}
			$name = $kehu['companyName'];
		} elseif ($field['target'] == "订单") {
			if (!$this->power("order", "newFollow")) {
				$json['warn'] = "你没有新增订单跟进的权限";
			} else {
				$buyCar = query("buyCar", " id = '$field[targetId]' ");
				if (empty($buyCar['id'])) {
					$json['warn'] = "未找到此订单";
				} else {
					$finger = 2;
				}
			}
			$name = $buyCar['name'];
		} else {
			$json['warn'] = "未知跟进对象";
		}
		if ($finger == 2) {
			$warn = insert(array("table" => "follow", "field" => $field));
			if ($warn == "新增成功") {
				//更新待办事项
				update(array(
					"table" => "backlog",
					"field" => array("workFlow" => "已处理", "updateTime" => date("Y-m-d H:i:s")),
					"where" => " stid = '" . $this->control['stid'] . "' and type = '$backlogType' and typeId = '$field[targetId]' and workFlow = '处理中' ",
				));
				//如果有提交下次跟进日
				if (!empty($this->post['startDay'])) {
					backlogIn(array(
						"stid"     => $this->control['stid'],
						"type"     => $backlogType,
						"typeId"   => $field['targetId'],
						"workFlow" => "处理中",
						"text"     => "您有一个" . $field['target'] . "【" . $name . "】需要跟进",
						"startDay" => $this->post['startDay'],
					));
				}
				//添加日志
				$text = $this->control['name'] . "新增了一条跟进：" . $field['target'] . "【" . $field['targetId'] . "】：" . $this->post['text'];
				logText(array("type" => "follow", "text" => $text));
				//提示
				$json['warn'] = "success";
			} else {
				$json['warn'] = $warn;
			}
		}
		//返回
		return $json;
	}
	
	/*删除跟进记录*/
	public function del()
	{
		//赋值
		$follow = query("follow", " id = '" . $this->post['id'] . "' ");
		//判断
		if (empty($this->post['id'])) {
			$json['warn'] = "跟进记录ID号为空";
		} elseif (empty($follow['id'])) {
			$json['warn'] = "未找到此跟进记录";
		} elseif ($follow['stid'] != $this->control['stid']) {
			$json['warn'] = "这不是您的跟进记录";
//		}elseif($follow['target'] == "事务"){
//			if(!power("affair","followDel")){
//				$json['warn'] = "权限不足";
//			}
		} elseif ($follow['target'] == "客户") {
			if (!$this->power("client", "delFollow")) {
				$json['warn'] = "权限不足";
			}
		} elseif ($follow['target'] == "订单") {
			if (!$this->power("order", "delFollow")) {
				$json['warn'] = "权限不足";
			}
		} else {
			$json['warn'] = "未知跟进对象";
		}
		if (empty($json['warn'])) {
			//删除跟进记录
			delete(array("table" => "follow", "where" => "id = '" . $this->post['id'] . "'"));
			//添加日志
			$text = $this->control['name'] . "删除了一条跟进：" . $follow['text'];
			logText(array("type" => "follow", "text" => $text));
			//返回
			$json['warn'] = "success";
		}
		return $json;
	}
}

?>